package com.babystudy.service;


import java.io.IOException;
import java.sql.Date;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.view.document.AbstractPdfView;

import com.babystudy.dao.UserDao;
import com.babystudy.dao.UserDaoImpl;
import com.babystudy.pojo.User;
import com.babystudy.utils.MathGenerator;
import com.db.utils.SimpleDateFormatTransformat;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.RectangleReadOnly;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
 

public class MyMathService extends AbstractPdfView {
	 
	private UserDao userdao;
	protected static BaseFont bf = null;
	protected Document document; 
    private String number;
    private String size;

	
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	public String getSize() {
		return size;
	}
	public void setSize(String size) {
		this.size = size;
	}
	public MyMathService(String number,String size){
		this.number = number;
		this.size = size;
 //fonttitle_english = new Font(bf_english, 20, Font.NORMAL);
 document= new Document(new RectangleReadOnly(842F, 595F));
// System.out.println("========================================");  
 document.setPageSize(PageSize.A4.rotate());
	document.setMargins(10, 10, 10, 10);
	
	 
	}
	public void buildPdfDocument(Map<String, Object> arg0, Document document, PdfWriter arg2, HttpServletRequest arg3,
			HttpServletResponse response) throws Exception {
		
		System.out.println("================"+arg3.getParameter("numbers"));
		System.out.println("================"+arg3.getParameter("size"));
		User user = new User();
		userdao = new UserDaoImpl();
		UUID uuid = UUID.randomUUID();
		 String remoteAddr = arg3.getRemoteAddr();
	       String forwarded = arg3.getHeader("X-Forwarded-For");
	       String realIp = arg3.getHeader("X-Real-IP");
	       String uid = uuid.toString();
		   String ip_addr = getLocalIp(remoteAddr,forwarded,realIp);
		   String download_time = SimpleDateFormatTransformat.transformat();
		   user.setDownload_content(this.number+"-"+this.size);
		   user.setDownload_time(download_time);
		   user.setIp_addr(ip_addr);
		   user.setUid(uid);
		//   System.out.println(user+"  user...");
		//  userdao.saveDownload(user);
		document.open();
		//MathGenerator.do20(document,Integer.parseInt(this.number), Integer.parseInt(this.size)); // 1000����Ŀƽ����20ҳֽ�ϡ�
		MathGenerator.Multiplydo20(document, Integer.parseInt(this.number), Integer.parseInt(this.size));
		document.close();
		
	    String filename = "Math乘法.pdf";//��������ʱ�ͻ���pdf������     
		   //     filename = MyUtils.encodeFilename(filename, request);//���������ļ���
		       // response.setContentType("multipart/form-data"); 
        response.setContentType("application/pdf");     
		response.setHeader("Content-disposition", "attachment;filename=" + filename);
		
		//System.out.println(uuid.toString());
		
	}
	   protected static String getLocalIp(String remoteAddr,String forwarded,String realIp) {
	      

	       String ip = null;
	       if (realIp == null) {
	           if (forwarded == null) {
	               ip = remoteAddr;
	           } else {
	               ip = remoteAddr + "/" + forwarded.split(",")[0];
	           }
	       } else {
	           if (realIp.equals(forwarded)) {
	               ip = realIp;
	           } else {
	               if(forwarded != null){
	                   forwarded = forwarded.split(",")[0];
	               }
	               ip = realIp + "/" + forwarded;
	           }
	       }
	       return ip;
	   } 
	

	

}
